﻿using System.Web;
using AntDesign;
using Microsoft.AspNetCore.Components;
using SE.PO.CInterface.CuCoding.Signals;
using SE.PO.CInterface.Web.Models;
using SE.PO.CInterface.Web.Pages.Form;

namespace SE.PO.CInterface.Web.Pages.DB.Components;

public partial class SelectSignalCoding
{
    private List<SelectModelItem> _deviceTypes = new();

    private string? _selectedDeviceType;
    
    private string? _selectedMonitorInfoCode;

    private List<string> _monitorTypes = new();

    private string? _selectedMonitorType;

    private List<SelectModelItem> _monitorItems = new();
    
    private List<SelectModelItem> _alarmItems = new();
    
    private string? _selectedAlarmProperity;

    private SelectModelItem? _selectedMonitorItem;

    [Parameter] public string Width { get; set; }

    [Parameter] public EventCallback<string> SelectDeviceTypeChanged { get; set; }
    
    [Parameter] public EventCallback<string> SelectMonitorItemChanged { get; set; }

    private Task<SignalCodingDto[]?>? _oldTask;

    private List<SelectModelItem> _sources = new();

    private List<SignalCodingDto>? _signalCodingDtos = new();

    protected override async Task OnInitializedAsync()
    {
        _signalCodingDtos = (await Client.Get()!)?.ToList();
        var list = (from s in _signalCodingDtos
                select new SelectModelItem(s.DeviceType, s.DeviceCode)
            ).ToList();
        _deviceTypes = list.GroupBy(d => new {d.Label, d.Value}).Select(d => d.First()).ToList();
    }

    private async Task OnSelectedDeviceTypeChangedHandler(SelectModelItem item)
    {
        _monitorTypes = (from s in _signalCodingDtos
                where Equals(item.Label, s.DeviceType)
                select s.MonitoringType)
            .Distinct().ToList();
        await SelectDeviceTypeChanged.InvokeAsync(item.Value);
    }

    private async Task OnSelectedMonitorItemChangedHandler(SelectModelItem item)
    {
        if (!string.IsNullOrWhiteSpace(_selectedMonitorInfoCode))
        {
            _selectedAlarmProperity = (from s in _signalCodingDtos
                where Equals(_selectedMonitorInfoCode, s.Code)
                select s.AlarmFlag).FirstOrDefault();
        }
        await SelectMonitorItemChanged.InvokeAsync(item.Value);
       
    }

    private void OnSelectedMonitorTypeChangedHandler(string value)
    {
        if (_signalCodingDtos != null)
        {
            var selectedDeviceTypeName = _signalCodingDtos.FirstOrDefault(s => s.DeviceCode.Equals(_selectedDeviceType))!.DeviceType ?? throw new ArgumentNullException("_signalCodingDtos.FirstOrDefault(s => s.DeviceCode.Equals(_selectedDeviceType)).DeviceType");
            _monitorItems = (from s in _signalCodingDtos
                where Equals(selectedDeviceTypeName, s.DeviceType)
                      && Equals(value, s.MonitoringType)
                select new SelectModelItem(s.MonitoringName, s.Code)).ToList();
        }
    }

    public string? SelectedMonitorInfoCode => _selectedMonitorInfoCode;
    
    public string? SelectedAlarmProperity => _selectedAlarmProperity;
    
    private async Task OnSearch(string value)
    {
        // if (_oldTask == null || _oldTask.IsCompleted)
        // {
        //     _sources.Clear();
        //     _oldTask = Client.Get(value);
        //     _sources = (from a in await _oldTask
        //         select new SelectModelItem($"{a.DeviceType}-{a.MonitoringName}", a.Code)).ToList();
        //
        //     await Task.Delay(1);
        //     StateHasChanged();
        // }
    }

    private readonly FormItemLayout _formItemLayout = new FormItemLayout
    {
        LabelCol = new ColLayoutParam
        {
            Xs = new EmbeddedProperty {Span = 24},
            Sm = new EmbeddedProperty {Span = 7},
        },

        WrapperCol = new ColLayoutParam
        {
            Xs = new EmbeddedProperty {Span = 24},
            Sm = new EmbeddedProperty {Span = 12},
            Md = new EmbeddedProperty {Span = 10},
        }
    };

    private readonly FormItemLayout _submitFormLayout = new FormItemLayout
    {
        WrapperCol = new ColLayoutParam
        {
            Xs = new EmbeddedProperty {Span = 24, Offset = 0},
            Sm = new EmbeddedProperty {Span = 10, Offset = 7},
        }
    };
}